package com.geek.controller;

import com.geek.bean.data.CouponExchangeData;
import com.geek.bean.BaseRespVo;
import com.geek.bean.PageData;
import com.geek.model.MarketCoupon;
import com.geek.service.CouponService;
import com.geek.service.CouponUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    CouponService couponService;
    @Autowired
    CouponUserService couponUserService;
    @GetMapping("list")
    public BaseRespVo list(Integer page,Integer limit){
        //查询
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            List<Map> couponList = couponService.couponList(userId,page, limit);
            PageData data = PageData.list(couponList);
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
    @GetMapping("mylist")
    public BaseRespVo mylist(Integer status,Integer page,Integer limit){
        //查询
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            PageData data =  couponUserService.list(userId,status,page,limit);
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            //兑换优惠卷
            CouponExchangeData exchangeData = couponService.exchange((String) map.get("code"));
            Boolean flag = exchangeData.getExchange();
            if (flag) {
                //如果兑换成功，向couponUser表中添加数据
                Integer couponId = exchangeData.getCouponId();
                int insert = couponUserService.insert(userId, couponId);
                if (insert ==0){
                    return BaseRespVo.fail("优惠卷领完了",742);
                }
                return BaseRespVo.ok(null);
            } else {
                //若失败，返回失败
                return BaseRespVo.fail("优惠卷不正确",742);
            }
        } else {
            return BaseRespVo.unAuthc();
        }
    }
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map){
        Integer id = (Integer) map.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            //查询coupon
            MarketCoupon coupon = couponService.selectById(id);
            //如果limit=0,不限制
            if (coupon.getLimit() != 0){
                //如果limit,默认是1，限领一张
                //判断couponuser中若有这张卷则不能领
                boolean flag = couponUserService.isList(id,userId,coupon.getLimit());
                if (flag) {
                    return BaseRespVo.fail("优惠卷已经领取过",740);
                }
            }
            int insert = couponUserService.insert(userId, id);
            if (insert == 0){
                return BaseRespVo.fail("优惠卷没有了",740);
            }
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.unAuthc();
        }
    }
    @GetMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId,Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            //查询还没使用的优惠卷
            List<Map> list = couponUserService.list(userId,0,cartId);
            PageData data = PageData.list(list);
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
}
